package com.sunshine.lnuplus.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.List;

public class DatabaseHelper extends SQLiteOpenHelper {
     private List<String> databaseList;

     public DatabaseHelper(@Nullable List<String> databaseList, @Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
         super(context, name, factory, version);
         this.databaseList = databaseList;
     }

     private DatabaseHelper(Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version){
         super(context,name,factory,version);
     }

     @Override
     public void onCreate(SQLiteDatabase sqLiteDatabase) {
         for(int i = 0;i<databaseList.size();i++){
             sqLiteDatabase.execSQL(databaseList.get(i));
         }
     }

     @Override
     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

     }

     public static SQLiteDatabase getDatabase(Context context,String databaseName){
         DatabaseHelper databaseHelper;
         SQLiteDatabase database;
         databaseHelper = new DatabaseHelper(context,databaseName,null,1);
         database = databaseHelper.getWritableDatabase();
         return database;
     }

     public static void addTimetableToDB(Context context, String className, String time, String teacher, String room,String color, String weeks){
         SQLiteDatabase database = getDatabase(context,"timetable.db");
         ContentValues values = new ContentValues();
         values.put("cname",className);
         values.put("time",time);
         if(teacher!=null){
             values.put("teacher",teacher);
         }
         if(room!=null){
             values.put("room",room);
         }
         if(color!=null){
             values.put("color",color);
         }else {
             values.put("color","0");
         }
         if(weeks!=null){
             values.put("weeks",weeks);
         }
         database.insert("timetable",null,values);
     }

     public static void updateTimetableToDB(Context context, String oldClassName, String className, String time, String teacher, String room, String color,String weeks){
         SQLiteDatabase database = getDatabase(context,"timetable.db");
         ContentValues values = new ContentValues();
         if(className!=null){
             values.put("cname",className);
         }
         if(teacher!=null){
             values.put("teacher",teacher);
         }
         if(room!=null){
             values.put("room",room);
         }
         if(color!=null){
             values.put("color",color);
         }else {
             values.put("color","0");
         }
         if(weeks!=null){
             values.put("weeks",weeks);
         }
         database.update("timetable",values,"cname=? and time=?",new String[]{oldClassName,time});
     }

     public static void deleteTimetableFromDB(Context context,String lessonName,String time){
         SQLiteDatabase database = getDatabase(context,"timetable.db");
         database.delete("timetable","cname=? and time=?",new String[]{lessonName,time});
     }

     public static void addCountDownToDB(Context context, String lessonName, String examTime ,String color, String examLocation){
         SQLiteDatabase database = getDatabase(context,"count_down.db");
         ContentValues values = new ContentValues();
         values.put("lesson_name",lessonName);
         values.put("exam_time",examTime);
         values.put("color",color);
         values.put("exam_location",examLocation);
         database.insert("count_down",null,values);
     }

    public static void updateCountDownToDB(Context context, String oldLessonName, String lessonName, String examTime, String color, String examLocation){
        SQLiteDatabase database = getDatabase(context,"count_down.db");
        ContentValues values = new ContentValues();
        values.put("lesson_name",lessonName);
        if(examTime!=null) values.put("exam_time",examTime);
        if(color!=null) values.put("color",color);
        if(examLocation!=null) values.put("exam_location",examLocation);
        database.update("count_down",values,"lesson_name=?",new String[]{oldLessonName});
    }

    public static void deleteCountDownFromDB(Context context, String lessonName){
        SQLiteDatabase database = getDatabase(context,"count_down.db");
        database.delete("count_down","lesson_name=?",new String[]{lessonName});
    }

     //插入老师
     public static void updateTeacher(Context context, String className,String teacher){
         SQLiteDatabase database = getDatabase(context,"timetable.db");
         ContentValues values = new ContentValues();
         if(teacher!=null){
             values.put("teacher",teacher);
         }
         database.update("timetable",values,"cname=?",new String[]{className});
     }

     public static void passwordToDB(Context context, String username, String password) {
         SQLiteDatabase database = getDatabase(context,"timetable.db");
         ContentValues values = new ContentValues();
         values.put("upassword",password);
         database.update("user",values,"uno=?",new String[]{username});
         values.put("uno",username);
         database.insert("user",null,values);
     }
}
